var ShortcutsStorage = {
  getShortcuts: function() {
    var shortcuts = localStorage.getItem('shortcuts') || '{}';
    return JSON.parse(shortcuts);
  },

  updateShortcuts: function(shortcuts) {
    localStorage.setItem('shortcuts', JSON.stringify(shortcuts));
  },

  /**
   * Add a shortcut data into localStorage.
   * @param {String} id
   * @param {Object} shortcut
   *   required keys: name, path, iconId, themeName
   */
  addShortcut: function(id, shortcut) {
    var shortcuts = this.getShortcuts();
    if (!shortcuts[id]) {
      shortcuts[id] = shortcut;
      this.updateShortcuts(shortcuts);
    }
  },

  getShortcut: function(id) {
    var shortcuts = this.getShortcuts();
    return shortcuts[id];
  },

  /**
   * Update a shortcut data from localStorage.
   * @param {String} id
   * @param {Object} shortcut
   *   required keys: name, path, iconId, themeName
   */
  updateShortcut: function(id, shortcut) {
    var shortcuts = this.getShortcuts();
    if (shortcuts[id]) {
      shortcuts[id] = shortcut;
      this.updateShortcuts(shortcuts);
    }
  },
  
  removeShortcut: function(id) {
    var shortcuts = this.getShortcuts();
    if (shortcuts[id]) {
      delete shortcuts[id];
      this.updateShortcuts(shortcuts);
    }
  },

  getShortcutId: function(name, path) {
    var shortcuts = this.getShortcuts();
    for (var id in shortcuts) {
      var shortcut = shortcuts[id];
      if (shortcut.name == name && shortcut.path == path)
        return id;
    }
    return null;
  },

  /**
   * Check if specified named shortcut is been created.
   * @param name
   */
  isShortcutExistent: function(name) {
    var shortcuts = ShortcutsStorage.getShortcuts();
    for (var id in shortcuts) {
      if (shortcuts[id].name == name)
        return true;
    }
    return false;
  }
};